@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/data-table/data-table' as mdc-data-table;
@use '@material/data-table/data-table-theme' as mdc-data-table-theme;
@use '../mdc-helpers/mdc-helpers';
@use '../../material/core/theming/theming';


@mixin color($config-or-theme) {
  $config: theming.get-color-config($config-or-theme);
  // Save original values of MDC global variables. We need to save these so we can restore the
  // variables to their original values and prevent unintended side effects from using this mixin.
  $orig-selected-row-fill-color: mdc-data-table-theme.$selected-row-fill-color;
  $orig-divider-color: mdc-data-table-theme.$divider-color;
  $orig-row-hover-fill-color: mdc-data-table-theme.$row-hover-fill-color;
  $orig-header-row-text-color: mdc-data-table-theme.$header-row-text-color;
  $orig-row-text-color: mdc-data-table-theme.$row-text-color;
  $orig-sort-icon-color: mdc-data-table-theme.$sort-icon-color;
  $orig-sort-icon-active-color: mdc-data-table-theme.$sort-icon-active-color;
  $orig-stroke-color: mdc-data-table-theme.$stroke-color;

  @include mdc-helpers.mat-using-mdc-theme($config) {
    mdc-data-table-theme.$selected-row-fill-color: rgba(mdc-theme-color.prop-value(primary), 0.04);
    mdc-data-table-theme.$divider-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12);
    mdc-data-table-theme.$row-hover-fill-color: rgba(mdc-theme-color.prop-value(on-surface), 0.04);
    mdc-data-table-theme.$header-row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87);
    mdc-data-table-theme.$row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87);
    mdc-data-table-theme.$sort-icon-color: rgba(mdc-theme-color.prop-value(on-surface), 0.6);
    mdc-data-table-theme.$sort-icon-active-color:
        rgba(mdc-theme-color.prop-value(on-surface), 0.87);
    mdc-data-table-theme.$stroke-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12);

    @include mdc-data-table.core-styles($query: mdc-helpers.$mat-theme-styles-query);
  }

  // Restore original values of MDC global variables.
  mdc-data-table-theme.$selected-row-fill-color: $orig-selected-row-fill-color;
  mdc-data-table-theme.$divider-color: $orig-divider-color;
  mdc-data-table-theme.$row-hover-fill-color: $orig-row-hover-fill-color;
  mdc-data-table-theme.$header-row-text-color: $orig-header-row-text-color;
  mdc-data-table-theme.$row-text-color: $orig-row-text-color;
  mdc-data-table-theme.$sort-icon-color: $orig-sort-icon-color;
  mdc-data-table-theme.$sort-icon-active-color: $orig-sort-icon-active-color;
  mdc-data-table-theme.$stroke-color: $orig-stroke-color;
}

@mixin typography($config-or-theme) {
  $config: theming.get-typography-config($config-or-theme);
  @include mdc-helpers.mat-using-mdc-typography($config) {
    @include mdc-data-table.core-styles($query: mdc-helpers.$mat-typography-styles-query);
  }
}

@mixin density($config-or-theme) {
  $density-scale: theming.get-density-config($config-or-theme);
  .mat-mdc-table {
    @include mdc-data-table-theme.density($density-scale,
      $query: mdc-helpers.$mat-base-styles-query);
  }
}

@mixin theme($theme-or-color-config) {
  $theme: theming.private-legacy-get-theme($theme-or-color-config);
  @include theming.private-check-duplicate-theme-styles($theme, 'mat-mdc-table') {
    $color: theming.get-color-config($theme);
    $density: theming.get-density-config($theme);
    $typography: theming.get-typography-config($theme);

    @if $color != null {
      @include color($color);
    }
    @if $density != null {
      @include density($density);
    }
    @if $typography != null {
      @include typography($typography);
    }
  }
}
